<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" :title="title"></uni-navbar></view>

		<view class="search-view"><u-search class="search-input" @search="search" @custom="search"></u-search></view>

		<u-tabs class="btm-tabs" :list="tabsList" :current="tabsCurrent" @change="tabsChange"></u-tabs>

		<block v-if="tabsCurrent == 0">
			<view class="banner"><u-swiper :list="swiperList" mode="none" height="320" @click="swiperClick"></u-swiper></view>

			<view class="h2-row">
				<view class="h2-title">学习课程</view>
				<navigator hover-class="none" class="h2-col" url="/pagesB/xuexi/list">更多</navigator>
			</view>

			<view class="list-box" :style="{ background: loading == 'nodata' ? 'none' : '#fff' }">
				<block v-for="(item, index) of newsList" :key="item.id">
					<view class="list-info_02" v-if="index < 3" @click="navTo('/pagesB/xuexi/detail?id=' + item.id)">
						<view class="left">
							<view class="info-title">{{ item.title }}</view>
							<view class="info-btm">
								<view class="name clamp">{{ item.author ? item.author : item.createByName }}</view>
								<text class="line"></text>
								<view class="time">{{ item.createTime }}</view>
							</view>
						</view>
						<view class="right"><image class="image" :src="linksImg(item.coverImg)" mode="aspectFill"></image></view>
					</view>
				</block>
				<view v-if="loading == 'nodata'"><image src="/static/result-img/no-record.png" mode="widthFix"></image></view>
			</view>

			<!-- <view class="h2-row">
				<view class="h2-title">数字图书馆</view>
				<navigator hover-class="none" class="h2-col" url="/pagesB/xuexi/news">更多</navigator>
			</view> -->

			<view class="btn-add" v-if="$sysBtns('zsxxwz_add')" @click="navTo('/pagesB/xuexi/add')">
				<u-icon name="plus" color="#fff" size="40"></u-icon>
			</view>
		</block>

		<block v-if="tabsCurrent == 1">
			<view class="list-box" :style="{ background: loading == 'nodata' ? 'none' : '#fff' }">
				<view class="list-info_01" v-for="(item, index) of newsList" :key="item.id" @click="navTo('/pagesB/xuexi/detail?id=' + item.id)">
					<view class="info-title">{{ item.title }}</view>
					<view class="info-btm">
						<view class="btm-left clamp">
							<view class="name clamp">{{ item.author ? item.author : item.createByName }}</view>
							<text class="left-time">{{ item.createTime }}</text>
						</view>
					</view>
					<view class="info-btn">
						<u-button class="sub-btn" type="error" plain size="medium" shape="circle" @click="edit(item)">编辑</u-button>
						<u-button class="sub-btn" v-if="item.atTop == '0' && $sysBtns('zsxxlb_top')" type="error" plain size="medium" shape="circle" @click="toTop(item, '1')">设为轮播图</u-button>
						<u-button class="sub-btn" v-if="item.atTop == '1' && $sysBtns('zsxxlb_top')" type="default" plain size="medium" shape="circle" @click="toTop(item, '0')">取消设为轮播图</u-button>
						<u-button class="sub-btn" type="default" plain size="medium" shape="circle" @click="del(item)">删除</u-button>
					</view>
				</view>
				<u-loadmore :status="loading" />
			</view>
			<view class="btn-add" v-if="$sysBtns('zsxxwz_add')" @click="navTo('/pagesB/xuexi/add')"><u-icon name="plus" color="#fff" size="38"></u-icon></view>
		</block>

		<block v-if="tabsCurrent == 2">
			<view class="table-box">
				<u-table class="table-inner" :thStyle="{ backgroundColor: '#fff' }" border-color="#F8F8F8">
					<u-tr>
						<u-th>委员姓名</u-th>
						<u-th>浏览量</u-th>
						<u-th>总浏览时长</u-th>
					</u-tr>
					<view class="table-cell" v-for="(item, index) of newsList" :key="index">
						<u-tr>
							<u-td>{{ item.readUserName }}</u-td>
							<u-td>{{ item.readNum }}</u-td>
							<u-td>
								<text class="text" @click="navTo('/pagesB/xuexi/statistics?id=' + item.readUser)">{{ item.duration || 0 }}分钟</text>
							</u-td>
						</u-tr>
					</view>
				</u-table>
			</view>
		</block>

		<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>
	</view>
</template>

<script>
import { mapState } from 'vuex';
export default {
	data() {
		return {
			scrollTop: 0,
			tabsCurrent: 0,
			tabsList: [
				{
					name: '学习培训',
					value: 0
				}
			],
			colorMap: {
				0: '#0099ff',
				1: '#ff9900',
				2: '#19be6b'
			},
			nodeStatus: -1,
			title: '学习培训',
			newsList: [],
			swiperList: [
				{
					image: '/static/img/default_bg01.png'
				}
			],

			news: {
				type: '3',
				secType: '',
				nodeStatus: '',
				clientCode: '',
				title: ''
			},
			loading: 'loadmore'
		};
	},
	onLoad() {
		this.loadLogin();
		this.loadBanner();
		this.loadData();
	},
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.news.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;

				if (this.$sysMenus('dsxx_manage')) {
					this.tabsList.push({
						name: '学习课程管理',
						value: 1
					});
				}
				if (this.$sysMenus('dsxx_statistics')) {
					this.tabsList.push({
						name: '统计分析',
						value: 2
					});
				}

				this.pageNum = 1;
				this.pageSize = 10;
			} else {
				this.$logout();
			}
		},
		loadData(type = 'add') {
			if (type == 'add' && this.loading == 'loading') {
				return false;
			}
			if (type == 'add' && this.loading == 'nomore') {
				return false;
			}

			this.loading = 'loading';
			if (type != 'add') {
				this.pageNum = 1;
			}
			let url = '/dushuxuexi/article/list';
			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				type: this.news.type,
				secType: this.news.secType,
				nodeStatus: this.news.nodeStatus,
				title: this.news.title,
				clientCode: this.clientCode,
				userId: this.userId
			};

			if (this.tabsCurrent == 1 && !this.$sysMenus('zsxxmanage')) {
				data.createBy = this.userId;
			}
			if (this.tabsCurrent == 2) {
				url = '/dushuxuexi/articleReader/list';
			}

			this.$request(url, data, {
				load: true
			}).then((res) => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.newsList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.newsList = this.newsList.concat(res.data.records);
				this.loaded = true;
				if (this.newsList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		loadBanner() {
			let url = '/dushuxuexi/article/list';
			let data = {
				pageNum: 1,
				pageSize: 10,
				atTop: '1',
				type: '3',
				clientCode: this.clientCode,
				userId: this.userId
			};

			this.$request(url, data, {
				load: true
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				this.swiperList = [];
				res.data.records.forEach((item) => {
					if (item.coverImg) {
						this.swiperList.push({
							id: item.id,
							title: item.title,
							image: this.linksImg(item.coverImg)
						});
					}
				});
			});
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.type(this.tabsList[index].value);
		},
		search(e) {
			this.news.title = e;
			this.loadData('refresh');
		},
		type(e) {
			// this.nodeStatus = e;
			// this.news.nodeStatus = e || '';
			this.newsList = [];
			this.loadData('refresh');
		},
		edit(item) {
			uni.navigateTo({
				url: '/pagesB/xuexi/add?id=' + item.id
			});
		},
		del(item) {
			uni.showModal({
				title: '提示',
				content: '确认删除？',
				success: (res) => {
					if (res.confirm) {
						let url = '/dushuxuexi/article/del';
						this.$request(url, {
							ids: [item.id]
						}).then((result) => {
							this.$msg(result.msg);
							this.loadData('refresh');
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		toTop(item, e) {
			let url = '/dushuxuexi/article/setTop';
			this.$request(url, {
				atTop: e,
				id: item.id
			}).then((result) => {
				this.$msg(result.msg);
				item.atTop = e;
				// this.loadData('refresh');
			});
		},
		swiperClick(index) {
			// this.current = index;
			if (this.swiperList[index].id) {
				uni.navigateTo({
					url: '/pagesB/xuexi/detail?id=' + this.swiperList[index].id
				});
			}
		},
		navTo(url) {
			uni.navigateTo({
				url: url
			});
		},
		linksImg(url) {
			if (url) {
				return this.$baseUrl + '/file/download/preview/0?path=' + url.replace(/\\/g, '/');
			} else {
				return this.$staticImgRandom('/static/img/default_big.png');
			}
		}
	}
};
</script>

<style lang="scss">
.banner {
	// height: 465rpx;
	padding: 20rpx 30rpx;
	min-height: 320rpx;
	background-color: #fff;
}

.h2-row {
	margin: 0;
	padding: 20rpx 30rpx;
	border-bottom: 1rpx solid $u-border-color;
	background-color: #fff;
}

.list-box {
	padding: 0 30rpx;
}

.index-card {
	padding: 0 30rpx 30rpx;
	background-color: #fff;

	.card-link {
		.image {
			margin: 0 auto 22rpx;
			width: 160rpx;
			height: 160rpx;
		}

		.link-item {
			margin-top: 26rpx;
			font-size: 14px;
			line-height: 1;
			text-align: center;
		}
	}
}

.table-box {
	clear: both;

	.name {
		display: flex;
		flex-wrap: nowrap;
		justify-content: center;

		.text {
			color: #333;
			width: 260rpx;
			text-align: left;
		}
	}

	.text {
		color: #0196fa;
	}

	.break-all {
		word-break: break-all;
	}

	.image {
		margin-right: 20rpx;
		width: 80rpx;
		height: 80rpx;
	}
}
</style>
